//
//  ASJSkill.m
//  ASJ001
//
//  Created by zephyrhan on 11/13/11.
//  Copyright (c) 2011 Ebrothers tech. All rights reserved.
//

#import "ASJSkill.h"


#define COOL_STEP_DURATION  0.5


@implementation ASJSkill

@synthesize skillName, skillSprite, effectStage;
@synthesize skillPriority, currentPriority, skillPowser, coolTime, currentCoolTime;


- (id)initWithSkillName:(NSString*)name frameCount:(NSInteger)count inStage:(ASJStage*)stage {
    return [self initWithSkillName:name frameCount:count comboTarget:nil action:nil inStage:stage];
}


- (id)initWithSkillName:(NSString*)name frameCount:(NSInteger)count comboTarget:(id)target action:(SEL)action inStage:(ASJStage*)stage {
    assert(([name length] > 0) && (count < 10));
    
    if ((self = [super init])) {
        skillName = [name retain];
        frameCount = count;
        killMonster = 0;
        killMonsterCont = 0;
        
        comboTarget = [target retain];
        comboAction = action;
        
        skillPriority = 0;
        currentPriority = skillPriority;
        
        [self performSelectorOnMainThread:@selector(loadSkillSpriteToLayer:) withObject:stage.worldLayer waitUntilDone:YES];
        effectStage = stage;
    }
    
    return self;
}


- (void)dealloc {
    [comboTarget release];
    [skillName release];
    [skillAnimation release];
    [skillSprite release];
    
    [super dealloc];
}


- (void)loadSkillSpriteToLayer:(CCLayer*)layer {
    skillBatchNode = [CCSpriteBatchNode batchNodeWithFile:[NSString stringWithFormat:@"%@.png", skillName]]; 
    [layer addChild:skillBatchNode];
    
    NSMutableArray* skillFrames = [NSMutableArray array];
//    NSMutableArray* seq = [NSMutableArray array];
    CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
    [frameCache addSpriteFramesWithFile:[NSString stringWithFormat:@"%@.plist", skillName]];
    for(int i = 0; i < frameCount; ++i) { 
        [skillFrames addObject:[frameCache spriteFrameByName:[NSString stringWithFormat:@"%@000%d.png", skillName, i]]];       
    }
    CCAnimation* ani = [CCAnimation animationWithFrames:skillFrames delay:0.1f];
    
    skillAnimation = [[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:ani restoreOriginalFrame:YES]] retain];
    skillSprite = [[CCSprite spriteWithSpriteFrame:[frameCache spriteFrameByName:[NSString stringWithFormat:@"%@0000.png", skillName]]] retain];
}


- (void)fireSkillAtPosition:(CGPoint)pos {
    assert(skillSprite != nil);
    
    skillSprite.position = pos;
    
    currentPriority = skillPriority;
    
    [skillBatchNode performSelectorOnMainThread:@selector(addChild:) withObject:skillSprite waitUntilDone:YES];
    [skillSprite   performSelectorOnMainThread:@selector(runAction:) withObject:skillAnimation waitUntilDone:YES];
}


- (void)stopSkill {
    assert(skillSprite != nil);
    
    [skillSprite stopAllActions];
    [skillBatchNode removeChild:skillSprite cleanup:NO];
    
    currentPriority = -1;
}


- (void)skillEffect {
    currentPriority = -1;
    
    if (coolTime != 0) {
        currentCoolTime = coolTime;
        coolSkillTimer = [[NSTimer scheduledTimerWithTimeInterval:COOL_STEP_DURATION target:self selector:@selector(coolSkillStep) userInfo:nil repeats:YES] retain];
    }
}


- (void)coolSkillStep {
    currentCoolTime -= COOL_STEP_DURATION;
    
    if (currentCoolTime <= 0) {
        currentCoolTime = 0;
        [coolSkillTimer invalidate];
        [coolSkillTimer release];
    }
}

@end
